package eerbaar.v2.menu;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Date;

import eerbaar.v2.app.view.Standard;
import eerbaar.v2.graphics.rect.Rectangle2;

public class Slide {

    private final long startTime;

    public Slide() {
        this.startTime = new Date().getTime();
    }

    private int calcPosition(Rectangle rectangle) {

        final double PERIOD = 1000; // 1 second
        final long now = new Date().getTime();
        final long age = (now - this.startTime);
        final double ageSecond = (age % PERIOD);
        final double normalizedAge = ageSecond / PERIOD;

        double normalizedPosition = Math.sin((Math.PI * 2) * normalizedAge);
        normalizedPosition /= 2;
        double width = rectangle.getWidth();
        int position = (int) Math.round((width * normalizedPosition)
                + (width / 2));
        return position;
    }

    public void draw(Graphics graphics, Rectangle rectangle) {

        int position = calcPosition(rectangle);

        Rectangle2 rectangle2 = new Rectangle2(rectangle);
        int width = 3;
        int left = rectangle2.getLeft() + position;
        int top = rectangle2.getTop();
        int height = (int) rectangle2.getHeight();

        Rectangle slide = new Rectangle(left, top, width, height);

        graphics.setColor(Standard.RED);
        graphics.fillRect(slide.x, slide.y, slide.width, slide.height);
        // canvas.draw(Pen.RED.transparacy(0.5), Fill.RED.transparacy(0.5),
        // slide);
    }
}
